import operate from '@dfeidao/atom-nodejs/db/operate';
import log from '@dfeidao/atom-nodejs/logger/log';
import doc_code from '@dfeidao/atom-nodejs/msg/doc-code';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';
interface Message {
	filter: {
		hidden_source_feature_name: string;
		hidden_source_feature_code: string;
		control_data: string;
	};
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	// params: {};
	// headers: {};
	// captcha: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}
/* 表名	modify_record
标题	修改记录
字段名称	字段标题	字段类型	字段长度	是否为空	是否主键
_id	id	string	50	ⅹ	√
change_personnel	更改人	string	50	√	ⅹ
hidden_source_feature_no	隐患源特性编号	string	50	√	ⅹ
change_date	更改时间	timestamp	50	√	ⅹ
change_field	更改字段	string	32	√	ⅹ
change_before	更改前	string	32	√	ⅹ
change_after	更改后	string	32	√	ⅹ */

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:f:\实习\kh\src\wyf\zj-000004\s002,action_id:' + action_id);

	// 生成全局编码
	const res = await doc_code(action_id, session_id, systemid, 'BM0014321', '', 1);

	const now_date = new Date().getTime(); // 1551167783406
	const { exec, insert } = operate(action_id, session_id, systemid);
	insert('hidden_source_feature_set', { hidden_source_feature_no: res.result, hidden_source_feature_name: message.filter.hidden_source_feature_name, hidden_source_feature_code: message.filter.hidden_source_feature_code, control_data: message.filter.control_data, productid });

	insert('modify_record', { hidden_source_feature_no: res.result, change_field: "隐患源特性代码", change_before: "", change_after: message.filter.hidden_source_feature_code, change_personnel: 'weiwei', change_date: now_date, productid });
	insert('modify_record', { hidden_source_feature_no: res.result, change_field: "隐患源特性名称", change_before: "", change_after: message.filter.hidden_source_feature_name, change_personnel: 'weiwei', change_date: now_date, productid });
	insert('modify_record', { hidden_source_feature_no: res.result, change_field: "控制数据", change_before: "", change_after: message.filter.control_data, change_personnel: 'weiwei', change_date: now_date, productid });

	const r = await exec();
	log('Service end path:f:\实习\kh\src\wyf\zj-000004\s002,action_id:' + action_id);
	return {
		data: r
	};
}
